/**
 * 简历销售收入 --用户类型
 * 
 *  */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/bar');
    require('echarts/chart/pie');
    
    var ecConfig = require('echarts/config');
    var zrUtil = require('zrender/tool/util');
    var zrnumber = require('echarts/util/number');

    var dateGrp = 'stat_date'; //没有日月周的选项

    var myChart_pie = ec.init(document.getElementById('income_usertype_pie'));
	var myChart_bar = ec.init(document.getElementById('income_usertype_bar'));

	
var option_bar = {
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
        },
        formatter: function (params){
            return params[0].name + '<br/>'
                   + params[1].seriesName + ' : ' + zrnumber.addCommas(params[1].value) + '<br/>'
                   + params[0].seriesName + ' : ' +  zrnumber.addCommas(params[1].value + params[0].value);
        }
    },
    legend: {
        selectedMode:false,
        data:['总收入', '现金收入']
    },
    toolbox: {
        show : true,
        orient:'vertical',
        y:'bottom',
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    calculable : true,
    dataZoom: {
            show: true,
            height: 20,
            realtime : true,
            start: 90
    }, 
    xAxis : [
        {
            type : 'category',
            data : ['2015-08-01','2015-08-02','2015-08-03','2015-08-04','2015-08-05','2015-08-06']
        }
    ],
    yAxis : [
        {
            type : 'value',
            boundaryGap: [0, 0.1]
        }
    ],
    series : [
        {
            name:'现金收入',
            type:'bar',
            stack: 'sum',
            barCategoryGap: '50%',
            itemStyle: {
                normal: {
                    color: 'gray',
                    barBorderColor: 'tomato',
                    barBorderWidth: 6,
                    barBorderRadius:0,
                    label : {
                        show: true, position: 'insideTop'
                    }
                }
            },
            data:[260, 200, 220, 120, 100, 80]
        },
        {
            name:'总收入',
            type:'bar',
            stack: 'sum',
            itemStyle: {
                normal: {
                    color: 'tomato',
                    barBorderColor: 'tomato',
                    barBorderWidth: 6,
                    barBorderRadius:0,
                    label : {
                        show: true, 
                        position: 'top',
                        formatter: function (params) {
                            for (var i = 0, l = option_bar.xAxis[0].data.length; i < l; i++) {
                                if (option_bar.xAxis[0].data[i] == params.name) {
                                     return zrnumber.addCommas(option_bar.series[0].data[i] + params.value);
                                }
                            }
                        },
                        textStyle: {
                            color: 'tomato'
                        }
                    }
                }
            },
            data:[40, 80, 50, 80,80, 70]
        }
    ]
};
                    

//饼图的数据模版
	var option_pie_one= {
		title : {
	    	text: '现金收入占比',
	    	x:'center',
	    	y:'45'
	    },
	    tooltip : {
	        trigger: 'item',
	        formatter: "{a} <br/>{b} : {c} ({d}%)"
	    },
	    legend: {
	        x : 'left',
	        data:[]
	    },
	    calculable : true,
	    toolbox: {
                show : true,
                orient:'vertical',
                y:'bottom',
                feature : {
                    mark : {show: false},
                    dataView : {show: true, readOnly: false},
                    magicType : {
                        show: true, 
                        type: ['pie', 'funnel'],
                        option: {
                            funnel: {
                                x: '25%',
                                width: '50%',
                                funnelAlign: 'left',
                                max: 1548
                            }
                        }
                    },
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
	    series : [
	        {
	            name:'',
	            type:'pie',
	            radius : '45%',
	            center: ['50%', 200],
	            data:[]
	        }
	    ]
	};

//带timeline的饼图模版   
	var option_pie = {
	    timeline : {
		    type:"number",
		    height:40,
		    symbolSize:3,
		        x:"1%",
		        x2:"1%",
		        width:"80%",
		        label:{
		                interval: 'auto',
		                formatter:function(param){
		    				return param;
		    	        }
		        },
		        data : ['']
	    },
	    options:[]
	};
      
	var legendData = ['总收入', '现金收入'];
         
	function showPie(){
		
		var name_type ='用户类型';
		
		myChart_pie.showLoading({
        	text: '正在努力的读取数据中...'
        });
        $.ajax({
        	url: '/dashboard/resume/ajax/getResumeIncome',
        	type: 'POST',
       		async: true,
        	data:{"name_type":name_type},
        	dataType: 'json',
         	success: function(data, textStatus) {

                    //设置饼图
                    setPie(data);
                    myChart_pie.hideLoading();
                	
                    //设置堆积图                   
                    show_bar(2);     
              //      myChart_bar.setOption(option_bar,true);
              //      myChart_bar.hideLoading();
                    
             },
             timeout: 5000,
             error: function() {
                 myChart_pie.hideLoading();
             }
          });
}

//设置饼图数据
	function setPie(data){
    	option_pie.options=[];
    	//饼图时间轴
        option_pie.timeline.data=data.timeline;
        option_pie.timeline.currentIndex=data.timeline.length-1;   
        
        //饼图数据
        $.each(data.dataList,function(index,value){
        	var option= zrUtil.clone(option_pie_one);
          
        	option.series[0].name=index;
            option.series[0].data = value; 
            
            option.legend.data = data.cate;        
                 
            option_pie.options.push(option);
         });
         
         myChart_pie.setOption(option_pie,true);

	}

    /**
    * 根据指标显示日数据
    * @param eventType= 2:第一次加载, 1:切换日，周，月刷新 , 0: 添加对比后又重新选择指标
    */
	function show_bar(eventType){
		
		var index_Type = $('#index_income_usertype').val();
		var name_type ='用户类型';
		
    	myChart_bar.showLoading({ text: '正在努力的读取数据中...' });
        $.ajax({
        	url:'/dashboard/resume/ajax/getResumeIncomebar',
            type: 'post',
            async: true,
            data:{"index_Type":index_Type,"name_type":name_type},
            dataType: 'json',
            success: function(data, textStatus) {
            	//X轴时间
                option_bar.xAxis[0].data = data.time;
                option_bar.legend.data = data.cate;
          
                //数据
                option_bar.series[0].data = data[legendData[0]];
                option_bar.series[1].data = data[legendData[1]];
                
                    
                //区域缩放               
                option_bar.dataZoom.start = (1-6/data.time.length)*100;
                var datezoom_local={start:(1-6/data.time.length)*100,end:100};
                
                //少于10个点，全显
                if(data.time.length<10){
                	datezoom_local = {start:0,end:100};
                    option_bar.dataZoom.start = 0;
                }

                if(myChart_bar.component.dataZoom){
                    myChart_bar.component.dataZoom.absoluteZoom(datezoom_local);
                }
                option_bar.dataZoom.zoomLock=true;
                
                myChart_bar.setOption(option_bar,true);
				myChart_bar.hideLoading();

            },
            error : function() {
                myChart_bar.hideLoading();
            }
        });
    }

	var zrUtil = require('zrender/tool/util');
        // console.log(zrUtil);

	function getShowDays(){
            return compDataZoom_old.option.xAxis[0].data;
        }
        
	var compDataZoom_old;
	var legend_SelectedMap={};  //需要记忆图例的选择状态
	function hideDataZoom(){
            legend_SelectedMap=myChart_bar.component.legend.getSelectedMap();
            compDataZoom_old=myChart_bar.component.dataZoom;    //记忆缩放
            option_bar.dataZoom.show=false;
            myChart_bar.setOption(option_line,false);
        }
  
	function getIndexByName(data,name) {
            var dataLength = data.length;
            for (var i = 0; i < dataLength; i++) {
                if (data[i] == name || typeof data[i].value != 'undefined' && data[i].value == name) {
                    return i;
                }
            }
            return -1;
        }
        
        
	function formatTime(time){
            return time;
        }
       
	//绑定chenge事件
        $("#index_income_usertype").change(function(){
            var type = 2;    
            show_bar(type);
           
        });
        
        //绑定chenge事件
        $("#city_income_usertype").change(function(){
            var type = 2;
             showPie();
        });
        
    
       $("#refresh_income_usertype_pie_btn").click(function(){
            showPie();
        });
    
     var EC={
            showPie:showPie,
            resize:function(){myChart_pie.resize();myChart_bar.resize();},
            hideDataZoom:hideDataZoom,   
            getShowDays:getShowDays     
        };
        
        //EC.show(businessName);
        return EC;
});